package cn.krund.lyc.hotlerobot.activity.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.List;

import cn.krund.lyc.hotlerobot.R;
import cn.krund.lyc.hotlerobot.activity.adapter.GridSelectAdapter;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * @包名: cn.krund.lyc.hotlerobot.activity.view
 * @作者: 刘育成
 * @时间: 2018/3/29  11:06
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: GridSelectDialog
 */

public class GridSelectDialog extends Dialog {
    public GridSelectDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 设置 Dialog的大小
     *
     * @param x 宽比例
     * @param y 高比例
     */
    public void setDialogWindowAttr(double x, double y, Activity activity) {
        if (x < 0 || x > 1 || y < 0 || y > 1) {
            return;
        }
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        WindowManager manager = activity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;
        lp.gravity = Gravity.CENTER;
        lp.width = (int) (width * x);
        lp.height = (int) (height * y);
        this.getWindow().setAttributes(lp);
    }

    public static class Builder {
        private Context mContext;
        private GridSelectDialog mDialog;
        private View mLayout;
        private GridView mGridView;
        DialogSearchView searchView;
        ImageButton searchBtn;
        ImageButton closeBtn;
        private String title;
        private List<String> listData;
        private OnSelectedListiner selectedListiner;


        public Builder(Context context) {
            this.mContext = context;
            mDialog = new GridSelectDialog(context, R.style.selectDialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            mLayout = inflater.inflate(R.layout.dialog_select_grid, null);
            mGridView = (GridView) mLayout.findViewById(R.id.gridview);
            searchView = (DialogSearchView) mLayout.findViewById(R.id.searchView);
            closeBtn = (ImageButton) mLayout.findViewById(R.id.imb_dialog_select_close);
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        public void setListData(List<String> listData) {
            this.listData = listData;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private GridSelectDialog create() {
            final GridSelectAdapter adapter = new GridSelectAdapter(mContext, listData);
            mGridView.setAdapter(adapter);
            mGridView.invalidate();
            searchView.setDialogSearchViewListener(new DialogSearchView.DialogSearchViewListener() {

                @Override
                public boolean onQueryTextChange(String text) {
                    updateLayout(searchItem(text));
                    return false;
                }
            });
            searchView.setDialogSearchViewListener(new DialogSearchView.DialogSearchViewListener() {

                @Override
                public boolean onQueryTextChange(String text) {
                    updateLayout(searchItem(text));
                    return false;
                }
            });
                    closeBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                }
            });
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    dialog.dismiss();
                }
            });
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectedListiner.onSelected(adapter.getItem(position) + "");
                    mDialog.dismiss();
                }
            });
            mDialog.setContentView(mLayout);
            //用户可以点击手机Back键取消对话框显示
            mDialog.setCancelable(true);
            //用户不能通过点击对话框之外的地方取消对话框显示
            mDialog.setCanceledOnTouchOutside(false);
            return mDialog;
        }

        public void updateLayout(List<String> newList) {
            final GridSelectAdapter sa = new GridSelectAdapter(mContext, newList);
            mGridView.setAdapter(sa);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectedListiner.onSelected(sa.getItem(position) + "");
                    mDialog.dismiss();
                }
            });
        }

        public List<String> searchItem(String name) {
            ArrayList<String> mSearchList = new ArrayList<String>();
            for (int i = 0; i < listData.size(); i++) {
                int index = listData.get(i).indexOf(name);
                // 存在匹配的数据
                if (index != -1) {
                    mSearchList.add(listData.get(i));
                }
            }
            return mSearchList;
        }

        public void setSelectedListiner(GridSelectDialog.Builder.OnSelectedListiner selectedListiner) {
            this.selectedListiner = selectedListiner;
        }

        public static abstract class OnSelectedListiner {
            public abstract void onSelected(String String);

        }

        public GridSelectDialog show() {
            create();
            mDialog.show();
            return mDialog;
        }
    }
}
